package com.example.dbcms.controller.admin;

import com.example.dbcms.annotation.Log;
import com.example.dbcms.domain.PropertyFee;
import com.example.dbcms.service.PropertyFeeService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/propertyFee")
@Controller
@Api("物业费用操作")
public class PropertyFeeController {

    @Autowired
    private PropertyFeeService propertyFeeService;

    /**
     * 跳转到物业费用记录页面,携带物业费用记录
     * @param propertyFee 查询物业费用条件
     * @param modelMap
     * @return
     */
    @ApiOperation("跳转到物业费用记录页面,携带物业费用记录")
    @GetMapping("/residentPayInfo")
    @Log(type="跳转到物业费用记录页面,携带物业费用记录")
    public String residentPayInfo(PropertyFee propertyFee,ModelMap modelMap) {
        PageInfo<PropertyFee> propertyFeePageInfo = propertyFeeService.selectAllPropertyFeeInfo(propertyFee);
        modelMap.put("propertyFeeList", propertyFeePageInfo.getList());
        modelMap.put("propertyFee",propertyFee);
        modelMap.put("pageInfo",propertyFeePageInfo);
        return "admin/property/index";
    }

    /**
     * 跳转到添加物业费用信息页面
     * @return
     */
    @ApiOperation("跳转到添加物业费用信息页面")
    @GetMapping("/addPropertyFee")
    @Log(type="跳转到添加物业费用信息页面")
    public String addPropertyFee(){
        return "/admin/property/add";
    }

    /**
     * 保存物业缴费信息,持久化到数据库
     * @param propertyFee 新增的物业缴费信息
     * @return
     */
    @ApiOperation("保存物业缴费信息,持久化到数据库")
    @PostMapping("/addPropertyFee")
    @ResponseBody
    @Log(type="保存物业缴费信息")
    public ResultVo addPropertyFeeToDb(PropertyFee propertyFee){
        int result = propertyFeeService.addPropertyFee(propertyFee);
        if(result == 1){
            return ResultVoUtil.success("添加成功");
        }
        return ResultVoUtil.error("添加失败");
    }

    /**
     * 通过id删除该条物业缴费记录
     * @param id
     * @return
     */
    @ApiOperation("通过id删除该条物业缴费记录")
    @GetMapping("/deletePropertyFee/{id}")
    @ResponseBody
    @Log(type="通过id删除该条物业缴费记录")
    public ResultVo deletePropertyFee(@PathVariable("id") int id){
        int result = propertyFeeService.deletePropertyFee(id);
        if(result==1){
            return ResultVoUtil.success("删除成功");
        }
        return ResultVoUtil.error("删除失败");
    }

    /**
     * 跳转到物业费用编辑页面
     * @param id
     * @return
     */
    @ApiOperation("跳转到编辑物业费用页面")
    @GetMapping("/editPropertyFee/{id}")
    @Log(type="跳转到编辑物业费用页面")
    public String editPropertyFee(@PathVariable("id") int id,ModelMap modelMap){
        modelMap.put("propertyFee",propertyFeeService.getPropertyById(id));
        return "admin/property/edit";
    }

    /**
     * 修改物业费用记录保存到数据库
     * @param propertyFee
     * @return
     */
    @ApiOperation("修改物业费用记录到数据库")
    @PostMapping("/editPropertyFee")
    @ResponseBody
    @Log(type="修改物业费用记录到数据库")
    public ResultVo savePropertyFee(PropertyFee propertyFee){
        int result = propertyFeeService.updatePropertyFee(propertyFee);
        if(result==1){
            return ResultVoUtil.success("修改成功");
        }
        return ResultVoUtil.error("修改失败");
    }

    /**
     * 通过携带的id查到相应记录跳转到物业费详细记录页面
     * @param id
     * @param modelMap
     * @return
     */
    @ApiOperation("跳转到物业费详细记录页面")
    @GetMapping("/detailPropertyFee/{id}")
    @Log(type="跳转到物业费详细记录页面")
    public String detailPropertyFee(@PathVariable("id") int id,ModelMap modelMap){
        modelMap.put("propertyFee",propertyFeeService.getPropertyById(id));
        return "admin/property/detail";
    }

    /**
     * 删除多条记录
     * @param ids 物业费用信息id列表
     * @return
     */
    @ApiOperation("删除多条居民信息")
    @PostMapping("/deleteMultiple")
    @ResponseBody
    @Log(type="删除多条居民信息")
    public ResultVo deleteMultiplePropertyFee(@RequestParam(value = "ids") List<Integer> ids){
        int result = propertyFeeService.deleteMultiplePropertyFee(ids);
        if(result==ids.size()){
            return ResultVoUtil.success("删除成功");
        }
        return ResultVoUtil.error("删除多条数据失败");
    }

    /**
     * 提醒用户缴纳物业费用
     * @return
     */
    @GetMapping("/remind")
    @ApiOperation("提醒用户缴纳停车费用")
    @ResponseBody
    @Log(type="提醒用户缴纳停车费用")
    public ResultVo remindPayPropertyFee(){
        propertyFeeService.remindPayPropertyFee();
        return ResultVoUtil.success("提醒成功");
    }
}
